package jcrosswords.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import jcrosswords.domain.Cell;
import jcrosswords.domain.ClueResponseInstance;
import jcrosswords.domain.Direction;
import jcrosswords.domain.Letter;
import jcrosswords.domain.Orientation;
import jcrosswords.util.CommonKeys;
import jcrosswords.util.PropertyManager;

public class CellPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	
	private Object v;
	
	private Cell cell;
	private JLabel valueLabel;
		
	private JLabel northWestLabel;
	private JLabel northLabel;
	private JLabel northEastLabel;
	private JLabel westLabel;
	private JLabel eastLabel;
	private JLabel southWestLabel;
	private JLabel southLabel; 
	private JLabel southEastLabel;
	
	private static ImageIcon northIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_north.png");
	private static ImageIcon northEastIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_north_east.png");
	private static ImageIcon northWestIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_north_west.png");
	private static ImageIcon southIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_south.png");
	private static ImageIcon southEastIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_south_east.png");
	private static ImageIcon southWestIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_south_west.png");
	private static ImageIcon eastNorthIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_east_north.png");
	private static ImageIcon eastSouthIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_east_south.png");
	private static ImageIcon eastIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_east.png");
	private static ImageIcon westNorthIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_west_north.png");
	private static ImageIcon westSouthIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_west_south.png");
	private static ImageIcon westIcon = new ImageIcon(PropertyManager.getInstance().get(CommonKeys.DESIGN_IMAGE_PATH)+"/img/arrow_west.png");
	
	
	public CellPanel(Cell cell) {
		setCell(cell);
		init();
	}

	public Cell getCell() {
		return cell;
	}

	public void setCell(Cell cell) {
		this.cell = cell;
	}
	
	private void init() {
		setBackground(Color.BLACK);
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		northWestLabel = new JLabel();
		c.gridx = 0;
		c.gridy = 0;
		add(northWestLabel, c);
		
		northLabel = new JLabel();
		c.gridx = 1;
		c.gridy = 0;
		add(northLabel, c);
		
		northEastLabel = new JLabel();
		c.gridx = 2;
		c.gridy = 0;
		add(northEastLabel, c);
		
		westLabel = new JLabel();
		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.LINE_START;
		add(westLabel, c);
		
		eastLabel = new JLabel();
		c.gridx = 2;
		c.gridy = 1;
		c.anchor = GridBagConstraints.LINE_END;
		add(eastLabel, c);
		
		southWestLabel = new JLabel();
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.CENTER;
		add(southWestLabel, c);
		
		southLabel = new JLabel();
		c.gridx = 1;
		c.gridy = 2;
		add(southLabel, c);
		
		southEastLabel = new JLabel();
		c.gridx = 2;
		c.gridy = 2;
		add(southEastLabel, c);
		
		valueLabel = new JLabel();
		
		refresh();

		c.gridx = 1;
		c.gridy = 1;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(valueLabel, c);
	}
	
	public void refresh() {
		setBackground(Color.BLACK);
		clearAllIcons();
		valueLabel.setText(null);
		v = cell.getValue();
		if(v != null) {
			if(v instanceof ClueResponseInstance) {
				setBackground(Color.LIGHT_GRAY);
				//setAllBackgrounds(Color.GRAY);
				valueLabel.setText("<html>" + ((ClueResponseInstance)v).getClueResponse().getClue() + "</html>");
				valueLabel.setFont(new Font(null, 0, 12));
			}
			if(v instanceof Letter) {
				setBackground(Color.WHITE);
				//setAllBackgrounds(Color.WHITE);
				valueLabel.setText(v.toString().toUpperCase());
				valueLabel.setFont(new Font(null, 0, 40));
				valueLabel.setHorizontalAlignment(SwingConstants.CENTER);
				if(cell.getFirstResponseOwners().size() > 0) {
					for(ClueResponseInstance cri: cell.getFirstResponseOwners()) {
						if(cri.getOrientation() == Orientation.NORTH) {
							if(cri.getDirection() == Direction.NORTH) {
								southLabel.setIcon(CellPanel.northIcon);
							}
							if(cri.getDirection() == Direction.EAST) {
								southWestLabel.setIcon(CellPanel.northEastIcon);
							}
							if(cri.getDirection() == Direction.WEST) {
								southEastLabel.setIcon(CellPanel.northWestIcon);
							}
						}
						if(cri.getOrientation() == Orientation.SOUTH) {
							if(cri.getDirection() == Direction.SOUTH) {
								northLabel.setIcon(CellPanel.southIcon);
							}
							if(cri.getDirection() == Direction.EAST) {
								northWestLabel.setIcon(CellPanel.southEastIcon);
							}
							if(cri.getDirection() == Direction.WEST) {
								northEastLabel.setIcon(CellPanel.southWestIcon);
							}
						}
						if(cri.getOrientation() == Orientation.EAST) {
							if(cri.getDirection() == Direction.NORTH) {
								westLabel.setIcon(CellPanel.eastNorthIcon);
							}
							if(cri.getDirection() == Direction.SOUTH) {
								westLabel.setIcon(CellPanel.eastSouthIcon);
							}
							if(cri.getDirection() == Direction.EAST) {
								westLabel.setIcon(CellPanel.eastIcon);
							}
						}
						if(cri.getOrientation() == Orientation.WEST) {
							if(cri.getDirection() == Direction.NORTH) {
								eastLabel.setIcon(CellPanel.westNorthIcon);
							}
							if(cri.getDirection() == Direction.SOUTH) {
								eastLabel.setIcon(CellPanel.westSouthIcon);
							}
							if(cri.getDirection() == Direction.WEST) {
								eastLabel.setIcon(CellPanel.westIcon);
							}
						}
					}
				}
			}
		}
	}
	
	private void clearAllIcons() {
		northWestLabel.setIcon(null);
		northLabel.setIcon(null);
		northEastLabel.setIcon(null);
		westLabel.setIcon(null);
		eastLabel.setIcon(null);
		southWestLabel.setIcon(null);
		southLabel.setIcon(null); 
		southEastLabel.setIcon(null);
	}
	
	private void setAllBackgrounds(Color c) {
		valueLabel.setBackground(c);
		northWestLabel.setBackground(c);
		northLabel.setBackground(c);
		northEastLabel.setBackground(c);
		westLabel.setBackground(c);
		eastLabel.setBackground(c);
		southWestLabel.setBackground(c);
		southLabel.setBackground(c); 
		southEastLabel.setBackground(c);
	}
	
	
}


